/**
 * 用户信息数据列表页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_USER_INFO");
    initSelect2();
    //加载数据列表
    loadGrid();

    $("#queryBtn").click(function(){
        query();
    });

    $("#resetBtn").click(function(){
        $('#searchForm')[0].reset();
        $('#searchForm').find('.select2-chosen').html('请选择');
    });

    $("#addBtn").click(function(){
        openAddPage();
    });
});

/**
 * 打开新增页面
 */
function openAddPage(){
    window.forward(springUrl+"/web/userInfo/addPage");
}

/**
 * 打开配置页面(默认角色配置页)
 */
function openConfigPage(uniqueNo){
    window.forward(springUrl+"/web/userInfo/roleConfigPage?uniqueNo="+uniqueNo);
}

/**
 * 打开修改页面
 */
function openEditPage(uniqueNo){
    window.forward(springUrl+'/web/userInfo/editPage?uniqueNo='+uniqueNo);
}

/**
 * 修改密码
 */
function changePwd(uniqueNo){
    window.forward(springUrl+"/web/userInfo/editPasswordPage?uniqueNo="+uniqueNo);
}

/**
 * 加载数据列表
 */
function loadGrid(){
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype: "POST",
        url: springUrl+"/web/userInfo/page",
        datatype: "json",
        autowidth: true,
        height: "100%",
        colModel: [
            {name:'uniqueNo',label:'唯一编号', width:100, sortable: false, hidden: true},
            {name:'isEnable',label:'是否启用', width:100, sortable: false, hidden: true},
            {name:'userAvatar',label:'头像',  width:100, fixed:true, sortable: false, align:'center',
                formatter:function(cellValue, options, rowObject){
                    var context = '<img class="nav-user-photo" src="/images/avatars/avatar.png" width="60" height="60"/>';
                    if(rowObject.userAvatar !== ""){
                        context = '<img class="nav-user-photo" src="/web/fileStorageInfo/download?uniqueNo='+rowObject.userAvatar+'" width="60" height="60"/>';
                    }
                    return context;
                }
            },
            {name:'userAccount',label:'用户账号',  width:100, sortable: false, align:'left'},
            {name:'userName',label:'用户名称',  width:100, sortable: false, align:'left'},
            {name:'userPhone',label:'用户电话',  width:100, sortable: false, align:'left'},
            {name:'operation', label:'操作', width:300, fixed:true, sortable:false, resize:false, align:'center',
                formatter:function(cellValue, options, rowObject){
                    var context = '';
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="openConfigPage(\''+rowObject.uniqueNo+'\')" title="配置">配置</button> ';
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="changePwd(\''+rowObject.uniqueNo+'\')" title="重置密码">重置密码</button> ';
                    if(rowObject.isEnable === 1){
                        context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="disableByUniqueNo(\''+rowObject.uniqueNo+'\')" title="停用">停用</button> ';
                    } else {
                        context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="enableByUniqueNo(\''+rowObject.uniqueNo+'\')" title="启用">启用</button> ';
                    }
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="openEditPage(\''+rowObject.uniqueNo+'\')" title="修改">修改</button> ';
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="deleteByPrimaryKey(\''+rowObject.uniqueNo+'\')" title="删除">删除</button> ';
                    return context;
                }
            }
        ],
        rownumbers: true,//添加左侧行号
        viewrecords : true,
        rowNum: 10,
        rowList: [10,20,30],
        sortname: "create_time",
        sortorder: "desc",
        altRows: true,
        pager : "#gridPager",
        jsonReader: { //server返回Json解析设定
            root: "content", //对于json中数据列表
            page: "pageNum",
            total: "total",
            records: "records"
        },
        gridComplete: function (xhr) {
            var gridTable = $("#gridTable");
            var ids = gridTable.getDataIDs();
            for(var i=0;i<ids.length;i++){
                var rowData = gridTable.getRowData(ids[i]);
                if(rowData.isEnable === "0"){
                    $('#'+ids[i]).find("td").addClass("bg-red");
                }
            }
        }
    });
    //navButtons
    gridTable.jqGrid('navGrid',"#gridPager",
        {
            add: false,
            addicon : 'fa-plus purple',
            edit: false,
            editicon : 'fa-pencil blue',
            del: false,
            delicon : 'fa-trash red',
            view: false,
            viewicon : 'fa-search-plus orange',
            search: false,
            searchicon : 'fa-search grey',
            refresh: true,
            refreshicon : 'fa-refresh green'
        }
    );
    updatePagerIcons(this);
    resizeJqGrid("gridTable");
}

/**
 * 查询
 */
function query(){
    if($('#searchForm').trigger("validate").isValid()){
        var gridParam = {
            url: springUrl+"/web/userInfo/page",
            postData:{
                userAccount: $("#userAccount").val(),
                userNameLike: $("#userNameLike").val(),
                isLocked: $("#isLocked").val()
            },
            page:1
        };
        $("#gridTable").jqGrid("setGridParam", gridParam).trigger("reloadGrid");
    }
}

/**
 * 启用
 */
function enableByUniqueNo(uniqueNo){
    Alert.confirm("确定启用该账号吗？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/userInfo/enableByUniqueNo",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("解锁成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    })
}

/**
 * 停用
 */
function disableByUniqueNo(uniqueNo){
    Alert.confirm("确定停用该账号吗？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/userInfo/disableByUniqueNo",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("锁定成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    })
}

/**
 * 根据主键删除
 */
function deleteByPrimaryKey(uniqueNo) {
    Alert.confirm("您确定删除该条信息？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/userInfo/delete",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("执行成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    })
}



